package com.example.cinema.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.example.cinema.BR;
import com.example.cinema.R;
import com.example.cinema.bindingdata.CommentShortData;

import java.util.List;

    public class ShortCommentAdapter extends BaseAdapter {

    private Context context;
    private List<CommentShortData> datalist;
    private int layoutid;
    private LayoutInflater inflater;
    public ShortCommentAdapter(Context context, List<CommentShortData> datalist, int layoutid) {
        this.context = context;
        this.datalist = datalist;
        this.layoutid = layoutid;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private ViewDataBinding dataBinding;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            dataBinding = DataBindingUtil.inflate(inflater,layoutid,parent,false);
            convertView = dataBinding.getRoot();
        }
        ImageView zan  = convertView.findViewById(R.id.zanbtns);
        zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(zanListener!=null)
                {
                    zanListener.ZanClick(zan,position,datalist.get(position));
                }
            }
        });
        dataBinding.setVariable(BR.sdata,datalist.get(position));
        return convertView;
    }

    //外部接口
    public interface OnZanListener{
        void ZanClick(ImageView view, int positon,Object datas);
    }
    private OnZanListener zanListener;
    public void SetZanListener(OnZanListener listener)
    {
        zanListener = listener;
    }
}
